package com.intelligentmeterreadingsys;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.intelligentmeterreadingsys.checkwork.activity.CheckWorkMainActivity;
import com.intelligentmeterreadingsys.otherequipment.OtherequipmentMainActivity;
import com.intelligentmeterreadingsys.report.activity.ReportMainActivity;
import com.intelligentmeterreadingsys.service.TimingLocationService;
import com.intelligentmeterreadingsys.settingsys.activity.SettingSysActivity;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterbanding.activity.WaterBandingMainActivity;
import com.intelligentmeterreadingsys.waterlist.activity.WaterListMainActivity;
import com.intelligentmeterreadingsys.waterlist.reviewdata.activity.ReviewMainActivity;


import dbhelper.DBOpenHelper;
import dbhelper.XmlNameHelper;

/**
 * 软件主界面
 * @author king
 *
 */
public class MainActivity extends Activity implements OnClickListener {
	public Button button1;//考勤
	
	public Button button2;//系统设置
	
	public Button button3;//退出系统
	
	public Button button4;//表册管理
	
	public Button button5;//复查管理
	
	public Button button6;//工程管理
	
	public boolean isSafe=false;
	
	public SQLiteDatabase db;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("OnCreate");
        setContentView(R.layout.main);
        button1=(Button)findViewById(R.id.checkwork);
        button2=(Button)findViewById(R.id.settingsys);
        button3=(Button)findViewById(R.id.exitsys);
        button4=(Button)findViewById(R.id.waterlist);
        button6=(Button)findViewById(R.id.Report);
        button5=(Button)findViewById(R.id.usermanager);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button4.setOnClickListener(this);
        button3.setOnClickListener(this);
        button2.setOnClickListener(this);
        button1.setOnClickListener(this);
        TelephonyManager tm = (TelephonyManager)getSystemService(MainActivity.TELEPHONY_SERVICE);               
        String imsi =tm.getSubscriberId();
        //将手机卡IMSI存入XML
        SharedPreferenceUtil sp=new SharedPreferenceUtil(this);
        sp.insertValue(XmlNameHelper.IMSI, imsi);
        String gpsdate=sp.getValue(XmlNameHelper.GPSDATE);
        if(gpsdate==null||gpsdate.equals("")){
        	sp.insertValue(XmlNameHelper.GPSDATE, "0");
        	sp.insertValue(XmlNameHelper.GPSLATITUDE, "0.0");
        	sp.insertValue(XmlNameHelper.GPSLONGITUDE, "0.0");
        }
        String safetime=sp.getValue(XmlNameHelper.safetytime);
        if(safetime==null||safetime.equals("")){
        	 sp.insertValue(XmlNameHelper.isSafe, true);
        }else{
        	 sp.insertValue(XmlNameHelper.isSafe, false);
        }
        
       
       
//        //启动定时下载
//        Intent intent=new Intent(MainActivity.this,WaterlistUploadService.class);
//       
//        startService(intent);
//        
    
        Intent intent1=new Intent(MainActivity.this, TimingLocationService.class);
        startService(intent1);
        DBOpenHelper helper=new DBOpenHelper(this);
        db= helper.getReadableDatabase();
        db.close();
    }
	@Override
	protected void onDestroy() {
		Intent intent=new Intent();
        intent.setAction("com.opengps");
       // stopService(intent);
		System.out.println("onDestroy");
		if(db!=null&&db.isOpen()){
			db.close();
		}
		super.onDestroy();
	}
	@Override
	public void onClick(View v) {
		Intent intent=null;
		switch (v.getId()) {
		case R.id.checkwork:
			
			intent=new Intent(MainActivity.this, CheckWorkMainActivity.class);
			startActivity(intent);
			break;
		case R.id.settingsys:
			intent=new Intent(MainActivity.this, SettingSysActivity.class);
			startActivity(intent);
			break;
			
		case R.id.exitsys:
			intent=new Intent(MainActivity.this, OtherequipmentMainActivity.class);
//			Bundle bundle=new Bundle();
//			bundle.putString("waterid", "");
//			intent.putExtras(bundle);
//			intent.putExtra("waterid", "");
			startActivity(intent);
			break;
		case R.id.waterlist:
			  intent=new Intent(MainActivity.this, WaterListMainActivity.class);
			  startActivity(intent);
			  break;
		case R.id.Report:
			  
			  intent=new Intent(MainActivity.this, ReportMainActivity.class);
			  startActivity(intent);
			  break;
		case R.id.usermanager:
			 intent=new Intent(MainActivity.this, ReviewMainActivity.class);
			  startActivity(intent);
			  break;
		default:
			break;
		}
		
	}
	


   
   public boolean showDialog(){
       
  	   LayoutInflater factory = LayoutInflater.from(this);  
         final View textEntryView = factory.inflate(R.layout.dialog, null);  
         final EditText editTextPass = (EditText) textEntryView.findViewById(R.id.txt_password);  
           
         AlertDialog.Builder ad1 = new AlertDialog.Builder(this);  
         ad1.setIcon(android.R.drawable.ic_dialog_info);  
         ad1.setView(textEntryView);
         ad1.setTitle("安全验证");
         ad1.setPositiveButton("确定", new  android.content.DialogInterface.OnClickListener() {
  		
  		@Override
  		public void onClick(DialogInterface dialog, int which) {
  			String pass=editTextPass.getEditableText().toString();
  			SharedPreferenceUtil sh=new SharedPreferenceUtil(MainActivity.this);
  			if(pass.equals(sh.getValue(XmlNameHelper.passWord))){
  				isSafe=true;
  			}else{
  				Toast.makeText(MainActivity.this, "密码错误", 1000).show();
  				isSafe=false;
  			}
  			
  		}
  	});
         ad1.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
  		
  		@Override
  		public void onClick(DialogInterface dialog, int which) {
  		       isSafe=false;
  			
  		}
  	});
         ad1.create();
         ad1.show();
		return isSafe;

     }
     
   public void SafeShow(Class<?> activityClass){
  	 Intent intent=null;
  	 SharedPreferenceUtil sh=new SharedPreferenceUtil(MainActivity.this);
		    boolean b=sh.getBoolean(XmlNameHelper.isSafe);
		    String pass=sh.getValue(XmlNameHelper.passWord);
		    if(!pass.equals("")||!b){
		    	boolean bb=showDialog();
		    	if(!bb){
		    		return;
		    	}else{
		    		intent=new Intent(this, activityClass);
				    startActivity(intent);
		    	}
		    }
		    intent=new Intent(this, activityClass);
		    startActivity(intent);
   }
}
